package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.dao.ExaminationPaperDao;
import com.jinzhi.jzweb.domain.ExaminationBatchComDO;
import com.jinzhi.jzweb.domain.ExaminationPaperDO;
import com.jinzhi.jzweb.service.ExaminationPaperService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 
 * <pre>
 * 考试试卷
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Service
public class ExaminationPaperServiceImpl extends CoreServiceImpl<ExaminationPaperDao, ExaminationPaperDO> implements ExaminationPaperService {

    @Override
    public List<String> selNumbers(Map map){
        return this.baseMapper.selNumbers(map);
    }


    /**
     * 试卷抽签接口
     * @param cbatchId
     * @return
     */
    @Override
    public Result<ExaminationPaperDO> trackDrawList(Long cbatchId) {
        //已经抽取试卷则不能再次抽取
        EntityWrapper<ExaminationPaperDO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("ebatch_id",cbatchId);
        wrapper1.eq("state","99");
        List<ExaminationPaperDO> examinationPaperDOS1 = this.selectList(wrapper1);
        if(null!=examinationPaperDOS1 && !examinationPaperDOS1.isEmpty()){
            return Result.fail("当前竞赛已经抽取试卷！");
        }
        //查询竞赛下是否有试卷
        EntityWrapper<ExaminationPaperDO> wrapper = new EntityWrapper<>();
        wrapper.eq("ebatch_id",cbatchId);
        List<ExaminationPaperDO> examinationPaperDOS = this.selectList(wrapper);
        if(null==examinationPaperDOS || examinationPaperDOS.isEmpty()){
            return Result.fail("当前竞赛下不存在试卷！无法抽签！");
        }
        //随机函数
        Random random = new Random();
        Collections.shuffle(examinationPaperDOS,random);
        int i = random.nextInt(examinationPaperDOS.size());

        //将抽中的试卷设置为考试试卷
        return Result.ok(examinationPaperDOS.get(i));
    }
}
